/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.platform.service.impl;

import cn.hutool.core.util.*;
import com.medusa.gruul.common.core.auth.model.constant.Keys;
import com.medusa.gruul.common.core.constant.RegexConstants;
import com.medusa.gruul.common.core.constant.TimeConstants;
import com.medusa.gruul.common.core.constant.enums.AuthCodeEnum;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.helper.RedisKeyHelper;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.redis.RedisManager;
import com.medusa.gruul.platform.conf.MeConstant;
import com.medusa.gruul.platform.constant.SmsConstant;
import com.medusa.gruul.platform.service.IMultiClientMobileService;
import com.medusa.gruul.sms.api.dto.SendSmsFeginDto;
import com.medusa.gruul.sms.api.feign.RemoteSmsSendService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

/**
 * @author 张治保
 * date 2021/12/3
 */
@Service
@RequiredArgsConstructor
public class MultiClientMobileServiceImpl implements IMultiClientMobileService {

    private final RemoteSmsSendService remoteSmsSendService;


    @Override
    public String sendSmsCode(String mobile, AuthCodeEnum type) {
        if (!ReUtil.isMatch(RegexConstants.REGEX_MOBILE_EXACT, mobile)) {
            throw new ServiceException("手机号错误", SystemCode.DATA_NOT_EXIST.getCode());
        }
        if (type == null) {
            throw new ServiceException("非法校验", SystemCode.FAILURE.getCode());
        }

        String redisKey = RedisKeyHelper.key("auth","mobile",type.name(),mobile);
        String code = RedisManager.getInstance().get(redisKey);
        if (StrUtil.isNotEmpty(code)) {
            return code;
        }
        code = RandomUtil.randomNumbers(6);
        SendSmsFeginDto sendSmsFeginDto = new SendSmsFeginDto();
        sendSmsFeginDto.setSmsSendTime(Calendar.getInstance().getTimeInMillis());
        sendSmsFeginDto.setSmsSendMobiles(mobile);
        sendSmsFeginDto.setSmsSendParam(code);
        sendSmsFeginDto.setSmsSendZone(SmsConstant.ZONE);
        sendSmsFeginDto.setSmsType(SmsConstant.SMS_TYPE_ALIYUN);
        sendSmsFeginDto.setSmsSendZone(SmsConstant.ZONE);
        sendSmsFeginDto.setSignId(SmsConstant.SIGN_ID);
        sendSmsFeginDto.setTemplateId(SmsConstant.TEMPLATE_ID);
        sendSmsFeginDto.setProviderId(SmsConstant.PROVIDER_ID);
        sendSmsFeginDto.setUserId(SmsConstant.USER_ID);
        Result order = remoteSmsSendService.createOrder(sendSmsFeginDto);
        if (MeConstant.STATUS_OK != order.getCode()) {
            throw new ServiceException("短信发送失败,".concat(order.getMsg()));
        }
        RedisManager.getInstance().set(redisKey, code, (int)TimeUnit.MILLISECONDS.toSeconds(TimeConstants.FIVE_MINUTES));
        return code;
    }

    @Override
    public String login(String mobile, String code) {
        if (!ReUtil.isMatch(RegexConstants.REGEX_MOBILE_EXACT, mobile)) {
            throw new ServiceException("手机号错误", SystemCode.DATA_NOT_EXIST.getCode());
        }
        if (StrUtil.isBlank(code) || code.length()!=6 || !NumberUtil.isNumber(code)){
            throw new ServiceException("验证码填写有误");
        }
        String cacheCode = RedisManager.getInstance().get(RedisKeyHelper.key("auth", "mobile", AuthCodeEnum.MINI_LOGIN.name(), mobile));
        if (StrUtil.isBlank(cacheCode)){
            throw new ServiceException("验证码已过期");
        }
        if (!code.equals(cacheCode)){
            throw new ServiceException("验证码不正确");
        }
        String loginId = IdUtil.simpleUUID();
        RedisManager.getInstance().set(
            RedisKeyHelper.key(Keys.USER_LOGIN,loginId),
            mobile,
            (int) TimeUnit.MILLISECONDS.toSeconds(TimeConstants.TEN_MINUTES)
        );
        return loginId;
    }
}
